from django.db import models
from django.forms.fields import CharField
from widgets import StaticInput
from fields import CascadeChoice
import uuid

class UUIDField( models.Field ):
    def __init__( self, **kwargs ):
        kwargs["max_length"] = 64
        kwargs["default"] = uuid.uuid4
        super(UUIDField,self).__init__(**kwargs)

    def formfield(self, **kwargs):
        defaults = {'widget': StaticInput }
        defaults.update(kwargs)
        return super(UUIDField,self).formfield(**defaults)
    
    def get_internal_type(self):
        return "CharField"

    def to_python(self, value):
        if isinstance(value, basestring):
            return value
        if value is None:
            if self.null:
                return value
            else:
                raise exceptions.ValidationError(
                    ugettext_lazy("This field cannot be null."))
        return smart_unicode(value)

class CascadeForeignKey( models.ForeignKey ):
    def formfield(self, **kwargs):
        return CascadeChoice()

